<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            height: 50px;
            width: 500px;
            border: 1px solid pink;
            text-align: center;
            line-height: 50px;
            margin: 10px auto;
        }
    </style>
</head>

<body>
    <div></div>
    <script>
        const div = document.querySelector('div')
        function getTime() {
            const date = new Date()
            let h = date.getHours()
            let m = date.getMinutes()
            let s = date.getSeconds()
            h = h <= 9 ? '0' + h : h
            m = m <= 9 ? '0' + m : m
            s = s <= 9 ? '0' + s : s
            return `${date.getFullYear()}年${date.getMonth()}月${date.getDay()}日 ${h}:${m}:${s}`

        }
        div.innerHTML = getTime()
        // 设置定时器
        setInterval(function () {
            div.innerHTML = getTime()
        }, 1000)















// 错误的写法
        // console.log(date.getFullYear())
        // console.log(typeof (date.getHours()))
        // if (date.getHours() <= 9 && date.getMinutes() <= 9) {
        //     document.write(`${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} 0${date.getHours()}:0${date.getMinutes()}:${date.getSeconds()}`)
        // }
        // else if (date.getMinutes() <= 9) {
        //     document.write(`${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:0${date.getMinutes()}:${date.getSeconds()}`)
        // }
        // else if (date.getHours() <= 9) {
        //     document.write(`${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} 0${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`)
        // }
    </script>
</body>

</html>